﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace LuckyBingoRcon.Players
{
	public class Player
	{
		protected string name = null;
		protected string clanTag = null;
		protected string guid = null;
		protected string ip = null;
        protected string lastMessage = null;

		protected int slotID = -1;
		protected int team = -1;
		protected int score = int.MinValue;
        protected int qPort = -1;
        protected int rate = -1;
		protected int ping = -1;

        TeamLookup teams = new TeamLookup();

		public Player() { }

        public int SlotID
        {
            get { return slotID; }
            set { slotID = value; }
        }

        public int Score
        {
            get { return score; }
            set { score = value; }
        }

        public int Ping
        {
            get { return ping; }
            set { ping = value; }
        }

        public string Guid
        {
            get { return guid; }
            set { guid = value; }
        }

        public string Name
		{
			get { return name; }
			set { name = value; }
		}

        public int Team
        {
            get { return team; }
            set { team = value; }
        }

        public string LastMessage
		{
            get { return lastMessage; }
            set { lastMessage = value; }
		}

        public string IP
        {
            get { return ip; }
            set { ip = value; }
        }

        public int Port
        {
            get { return qPort; }
            set { qPort = value; }
        }

        public int Rate
        {
            get { return rate; }
            set { rate = value; }
        }

        public string ClanTag
        {
            get { return clanTag; }
            set { clanTag = value; }
        }

        public override string ToString()
		{
			return name;
		}

        public static Player FromUglyString(string str)
        {
            Player player = new Player();
            List<string> args = new List<string>();
            string updated = str.Trim();
            int index = -1;
            for (int count = 0; count < 15; count++)
            {
                if (count.Equals(4))
                {
                    index = updated.LastIndexOf("^7");
                    if (index.Equals(-1))
                        break;
                    if (index > updated.Length - 2)
                        index = -1;
                    updated = updated.Remove(index, 2);
                }
                else
                {
                    index = updated.IndexOf(' ');
                }
                if (!index.Equals(-1))
                {
                    args.Add(updated.Substring(0, index));
                    updated = updated.Substring(index).TrimStart();
                }
                else
                {
                    index = count;
                    break;
                }
            }
            args.Add(updated);
            int num = -1;
            if (args.Count.Equals(10) && int.TryParse(args[0], out num))
            {
                player.SlotID = num;                        // slot
                if (int.TryParse(args[1], out num))         // score
                    player.Score = num;
                if (int.TryParse(args[2], out num))         // ping
                    player.Ping = num;
                if (!string.IsNullOrEmpty(args[3]))         // guid
                    player.Guid = args[3];
                if (!string.IsNullOrEmpty(args[4]))         // name
                {
                    index = args[4].IndexOf('^');
                    string name = args[4];
                    while(!index.Equals(-1))
                    {
                        if (index <= name.Length - 2)
                            name = name.Remove(index, 2);
                        else
                            name = name.Remove(index, 1);
                        index = name.IndexOf('^');
                    }
                    player.Name = name;
                }
                if (int.TryParse(args[5], out num))         // team
                    player.Team = num;
                if (!string.IsNullOrEmpty(args[6]))         // last message
                    player.LastMessage = args[6];
                if (!string.IsNullOrEmpty(args[7]))         // address
                    player.IP = args[7];
                if (int.TryParse(args[8], out num))         // qport
                    player.Port = num;
                if (int.TryParse(args[9], out num))         // rate
                    player.Rate = num;
            }
            else
                player = null;
            return player;
        }

        public DataGridViewRow ToRow(DataGridViewRow row, string mapName)
        {
            row.Cells[0].Value = slotID;
            row.Cells[1].Value = clanTag;
            row.Cells[2].Value = name;
            if(mapName != null)
            {
                if (team.Equals(1) && teams.GetTeamOneName(mapName) != null)
                    row.Cells[3].Value = teams.GetTeamOneName(mapName);
                else if (team.Equals(2) && teams.GetTeamTwoName(mapName) != null)
                    row.Cells[3].Value = teams.GetTeamTwoName(mapName);
                else if (team.Equals(3))
                    row.Cells[3].Value = "Spectate";
                else
                    row.Cells[3].Value = team;
            }
            else
                row.Cells[3].Value = team;
            row.Cells[4].Value = score;
            row.Cells[5].Value = ping;
            row.Cells[6].Value = guid;
            row.Cells[7].Value = ip;
            return row;
        }
	}
}
